// mine.js
var app = getApp()
import { formatMoney, outputmoney, formatData} from "../../utils/util.js";
var date = new Date();
var sliderWidth = 20;
// let _data =[]
Page({

  /**
   * 页面的初始数据
   */
  data: {
    years: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
    user: null,
    money: null,
    yearsList: [],
    monyList: [],
    monyStars: [],
    yearsIndex: parseInt(date.getMonth()),
    sliderOffset: 100,
    sliderLeft: 0,
    scrollView:'九月'
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let self = this
    self.dataTime()
    wx.getSystemInfo({
      success: function (res) {
        console.log(res)
        self.setData({
          sliderLeft: (res.windowWidth / self.data.years.length - sliderWidth) / 2,
          sliderOffset: res.windowWidth / 5
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let self = this
    let userObj = wx.getStorageSync('wechat_user')
    console.log(userObj)
    userObj.raty = formatMoney(userObj.raty)
    userObj.amount = formatMoney(userObj.amount, true)
    userObj.security = formatMoney(userObj.security, true)
    userObj.accumulative_count = formatMoney(userObj.accumulative_count, true)
    let moneys = parseFloat(userObj.amount) + parseFloat(userObj.security)
    let moneyPro = outputmoney(moneys)
    self.setData({ money: moneyPro })
    self.setData({ user: userObj })
    self.stars()
  },
  //日期
  dataTime: function () {
    let self = this
    let times = []
    let timeObj;
    var i, j, z
    var month = date.getMonth() + 1;
    for (i = month, j = 1; i > 0, j <= month; i-- , j++) {
      timeObj = {}
      timeObj.id = i;
      timeObj.name = this.convertToChinese(j)+"月";
      timeObj.index=j;
      times.push(timeObj);
    }
    self.setData({
      yearsList: times,
      // scrollView: toString(this.data.yearsIndex)
    })
    console.log(self.data.yearsList)
  },
  //数字转中文
  convertToChinese: function (num) {
    var n=["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"]
    var str = num.toString()
    var len = num.toString().length
    var C_Num = []
    for (var i = 0; i < len; i++) {
      C_Num.push(n[str.charAt(i)])
    }
    return C_Num.join('')
  },
  //获取钱包列表
  moneyList: function (e) {
    console.log(e)
    this.setData({
      yearsIndex: e.currentTarget.id
    })
    let yeaesId = e.currentTarget.dataset.yearsid
    let detailObj = { start: yeaesId, end: yeaesId, limit: 1000, page: 1 }
    console.log(detailObj)
    app.query("web/payments/payments_detail", detailObj).then(res => {
      console.log(res)
      let formatOrders = formatData(res.payments)
      this.setData({
        monyList: formatOrders
      })
    })
  },
  //星星判断
  stars: function () {
    let starsList = []
    let imgObj = { url: "../img/me/order_star.png" }
    if (this.data.user.raty != null) {
      this.data.user.raty = (parseInt(this.data.user.raty / 1))
      console.log(this.data.user.raty)
      switch (this.data.user.raty) {
        case 1: starsList.push(imgObj); break;
        case 2: starsList.push(imgObj, imgObj); break;
        case 3: starsList.push(imgObj, imgObj, imgObj); break;
        case 4: starsList.push(imgObj, imgObj, imgObj, imgObj); break;
        case 5: starsList.push(imgObj, imgObj, imgObj, imgObj, imgObj); break;
      }
      this.setData({
        monyStars: starsList
      })
      console.log(starsList)
    }
  },
  //详情
  mineDetail: function () {
    wx.navigateTo({
      url: '../mineDetail/mineDetail',
    })
  }
})